package hust.music.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hust.music.dao.JpaDao;
import hust.music.entity.Music;
import hust.music.entity.PlayList;
import hust.music.entity.User;

import hust.music.service.PlayListModService;
import hust.music.util.DateConvert;
import hust.music.util.DecimalConvert;
import hust.music.util.DynamicInfo;
import hust.music.util.RandomImage;

/**
 * PlayListModServiceImpl
 * 
 * @author Administrator
 * @since 1.0
 */
@Service("playListModService")
@Transactional
public class PlayListModServiceImpl implements PlayListModService {

	@Autowired
	private JpaDao jpaDao;

	@Override
	public PlayList getPlayList(Integer id) {
		List<PlayList> list = jpaDao.findAll(PlayList.class,
				"from PlayList p where p.id = ?", id);
		if (list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public User getUser(PlayList playList) {
		List<User> list = jpaDao
				.findAll(
						User.class,
						"select u from User u, PlayList p where p.user = u and p.id = ?",
						playList.getId());
		if (list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<DynamicInfo> getOtherPlayList(User user, PlayList playList) {
		List<PlayList> playLists = jpaDao
				.findAll(
						PlayList.class,
						"select p from PlayList p where p.user = ? and p.id <> ? order by p.time desc",
						new Object[] { user, playList.getId() });

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();
		for (PlayList p : playLists) {
			int count = jpaDao
					.findAll(
							Music.class,
							"select m from Music m where ? in elements(m.playLists)",
							p).size();
			DynamicInfo dynamicInfo = new DynamicInfo(p.getId(), p.getTitle(),
					p.getAvatar(), null, null, null, null, null, null,
					DateConvert.convertDateToString(p.getTime()), null, ""
							+ count);
			list.add(dynamicInfo);
		}

		return list;
	}

	@Override
	public List<Music> getMusic(PlayList playList) {
		List<Music> music = jpaDao.findAll(Music.class,
				"select m from Music m where ? in elements(m.playLists)",
				playList);

		return music;
	}

	@Override
	public int getTotalMusicCount(PlayList playList) {
		return jpaDao.findAll(Music.class,
				"select m from Music m where ? in elements(m.playLists)",
				playList).size();
	}

	@Override
	public List<DynamicInfo> searchMusic(String title) {
		List<Music> music = jpaDao
				.findAll(
						Music.class,
						"select m from Music m where m.title like ? order by m.uploadTime desc",
						"%" + title + "%");

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();

		for (Music m : music) {
			DynamicInfo dynamicInfo = new DynamicInfo(m.getId(), m.getTitle(),
					null, m.getUser().getId(), m.getUser().getNickname(), null,
					null, null, m.getPath(), null, null, null);
			list.add(dynamicInfo);
		}

		return list;
	}

	@Override
	public List<DynamicInfo> searchPlayList(String title) {

		List<PlayList> playList = jpaDao
				.findAll(
						PlayList.class,
						"select p from PlayList p where p.title like ? order by p.time desc",
						"%" + title + "%");

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();

		for (PlayList p : playList) {
			DynamicInfo dynamicInfo = new DynamicInfo(p.getId(), p.getTitle(),
					p.getAvatar(), p.getUser().getId(), p.getUser()
							.getNickname(), null, null, null, null, null, null,
					null);
			list.add(dynamicInfo);
		}

		return list;
	}

	@Override
	public boolean addPlayList(String title, String des, User user) {
		// TODO Auto-generated method stub
		try {
			PlayList list = new PlayList();
			list.setTitle(title);
			list.setDescription(des);
			list.setTime(new Date());
			list.setAvatar("./images/list/" + RandomImage.random() + ".jpg");
			User u = jpaDao.get(User.class, user.getId());
			list.setUser(u);
			jpaDao.save(list);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

	@Override
	public List<DynamicInfo> getUserPlayList(User user) {
		// TODO Auto-generated method stub
		List<PlayList> playLists = jpaDao.findAll(PlayList.class,
				"from PlayList p where p.user = ?", user);
		List<DynamicInfo> list = new ArrayList<DynamicInfo>();
		for (PlayList p : playLists) {
			DynamicInfo dynamicInfo = new DynamicInfo(p.getId(), p.getTitle(),
					null, null, null, null, null, null, null, DateConvert
							.convertDateToString(p.getTime()), null, null);
			list.add(dynamicInfo);
		}
		return list;
	}

	@Override
	public boolean addMusicToList(Integer listId, Integer musicId) {
		// TODO Auto-generated method stub
		try {
			PlayList playList = jpaDao.get(PlayList.class, listId);
			Music music = jpaDao.get(Music.class, musicId);
			if (music.getPlayLists().contains(playList)) {
				return false;
			}
			music.getPlayLists().add(playList);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List<DynamicInfo> findMusicByListening(int firstResult, int maxResult) {
		List<Music> music = jpaDao
				.findByPage(
						Music.class,
						"select m from Music m order by m.listenTimes desc, m.uploadTime desc, m.title asc",
						firstResult, maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();
		for (Music m : music) {
			DynamicInfo dynamicInfo = new DynamicInfo(m.getId(), m.getTitle(),
					m.getSinger(), m.getUser().getId(), m.getUser()
							.getNickname(), m.getAlbum(), null, m.getImage(),
					DecimalConvert.convert(m.getMark()), DateConvert
							.convertDateToString(m.getUploadTime()), m
							.getPath(), "" + m.getListenTimes());
			list.add(dynamicInfo);
		}

		return list;
	}

	@Override
	public List<DynamicInfo> findMusicByMark(int firstResult, int maxResult) {
		List<Music> music = jpaDao
				.findByPage(
						Music.class,
						"select m from Music m order by m.mark desc, m.uploadTime desc, m.title asc",
						firstResult, maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();
		for (Music m : music) {
			DynamicInfo dynamicInfo = new DynamicInfo(m.getId(), m.getTitle(),
					m.getSinger(), m.getUser().getId(), m.getUser()
							.getNickname(), m.getAlbum(), null, m.getImage(),
					DecimalConvert.convert(m.getMark()), DateConvert
							.convertDateToString(m.getUploadTime()), m
							.getPath(), "" + m.getListenTimes());
			list.add(dynamicInfo);
		}

		return list;
	}

	@Override
	public List<DynamicInfo> findMusicByTime(int firstResult, int maxResult) {
		List<Music> music = jpaDao
				.findByPage(
						Music.class,
						"select m from Music m order by m.uploadTime desc, m.title asc",
						firstResult, maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();
		for (Music m : music) {
			DynamicInfo dynamicInfo = new DynamicInfo(m.getId(), m.getTitle(),
					m.getSinger(), m.getUser().getId(), m.getUser()
							.getNickname(), m.getAlbum(), null, m.getImage(),
					DecimalConvert.convert(m.getMark()), DateConvert
							.convertDateToString(m.getUploadTime()), m
							.getPath(), "" + m.getListenTimes());
			list.add(dynamicInfo);
		}

		return list;
	}

	@Override
	public int findTotalMusic() {
		return jpaDao.findAll(Music.class, "select m from Music m").size();
	}

	@Override
	public List<DynamicInfo> findMusicByLabel(String label, int firstResult,
			int maxResult) {
		List<Music> music = jpaDao.findByPage(Music.class,
				"select m from Music m, Label l where l in elements(m.labels) and l.title = ?"
						+ " order by m.uploadTime desc, m.title asc", label,
				firstResult, maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();
		for (Music m : music) {
			DynamicInfo dynamicInfo = new DynamicInfo(m.getId(), m.getTitle(),
					m.getSinger(), m.getUser().getId(), m.getUser()
							.getNickname(), m.getAlbum(), null, m.getImage(),
					DecimalConvert.convert(m.getMark()), DateConvert
							.convertDateToString(m.getUploadTime()), m
							.getPath(), "" + m.getListenTimes());
			list.add(dynamicInfo);
		}

		return list;
	}

	@Override
	public int findTotalMusicByLabel(String label) {
		return jpaDao.findAll(
				Music.class,
				"select m from Music m, Label l where l in elements(m.labels) and l.title = ?"
						+ " order by m.uploadTime desc, m.title asc", label)
				.size();
	}

}
